package com.v2.activity;

import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.widget.DatePicker;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.jakewharton.rxbinding3.view.RxView;
import com.qmuiteam.qmui.widget.QMUITabSegment;
import com.v2.utils.TToast;
import com.zou.lotterylucky.R;
import com.zou.lotterylucky.activity.DanChangAdapter;
import com.zou.lotterylucky.activity.MySection;
import com.zou.lotterylucky.databinding.ActivityTotalBinding;
import com.zou.lotterylucky.utils.AidlUtil;
import com.zou.lotterylucky.utils.TimeCallBack;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.functions.Consumer;
import kotlin.Unit;

public class TotalActivity extends BaseActivity<ActivityTotalBinding> {

    private DanChangAdapter adapter;
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private boolean isPrinting = false;//正在打印

    @Override
    protected int layoutId() {
        return R.layout.activity_total;
    }

    @SuppressLint("CheckResult")
    @Override
    protected void onCreate() {
        List<MySection> set = new ArrayList<>();
        mBinding.topbar.setTitle(R.string.zongdan);
        mBinding.topbar.addLeftBackImageButton().setOnClickListener(v -> onBackPressed());

        adapter = new DanChangAdapter(set);
        mBinding.rcv.setLayoutManager(new LinearLayoutManager(this));
        mBinding.rcv.setAdapter(adapter);

        AidlUtil.Companion.getInstance().getTime("yyyy-MM-dd", new TimeCallBack() {
            @Override
            public void onError(String msg) {
                TToast.show(msg);
            }

            @Override
            public void onSuccess(String time) {
                mBinding.textDate.setText(time);
            }
        });

        Calendar calendar = Calendar.getInstance();

        DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                calendar.set(Calendar.YEAR, year);
                calendar.set(Calendar.MONTH, month);
                calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                mBinding.textDate.setText(dateFormat.format(calendar.getTime()));
                initData();
            }
        };

        RxView.clicks(mBinding.textDate).throttleFirst(1000, TimeUnit.MILLISECONDS).subscribe(unit -> {
            new DatePickerDialog(this, dateSetListener, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();
        });

        mBinding.tabs.addTab(new QMUITabSegment.Tab("KORSOU"));
        mBinding.tabs.addTab(new QMUITabSegment.Tab("FLAMINGO"));
        mBinding.tabs.addTab(new QMUITabSegment.Tab("DIARIO"));
        mBinding.tabs.addTab(new QMUITabSegment.Tab("ST"));
        mBinding.tabs.addOnTabSelectedListener(new QMUITabSegment.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int index) {
                selectData(index);
            }

            @Override
            public void onTabUnselected(int index) { }

            @Override
            public void onTabReselected(int index) { }

            @Override
            public void onDoubleTap(int index) { }
        });

        mBinding.tabs.selectTab(0);
        initData();

        RxView.clicks(mBinding.btnPrint).throttleFirst(600, TimeUnit.MILLISECONDS).subscribe(unit -> {
            if (isPrinting) return;
            if (!isNetworkAvailable(this)) {
                TToast.show(getString(R.string.internet_fail));
                return;
            }
            if (!AidlUtil.Companion.getInstance().isConnect()) {
                TToast.show(R.string.weilianjiadayinfuwu);
                return;
            }

            AidlUtil.Companion.getInstance().getTime("dd-MM-yyyy HH:mm:ss", new TimeCallBack() {
                @Override
                public void onError(String msg) {
                    TToast.show(msg);
                }

                @Override
                public void onSuccess(String time) {
                    if (time != null) {
                        goToPrint(time);
                    }
                }
            });
        });
    }

    /*打印*/
    private void goToPrint(String time) {

    }


    private void selectData(int index) {

    }

    private void initData() {

    }
}
